<?php
namespace app\admin\controller;

class Rule extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Rule';
    }
    //
    protected function _before_index(){
        $thead = [
            'id' => 'ID',
            'title' =>'规则名称',
            'name' =>'认证规则',
            'module_id' => '归属模块',
            'type' => '高级验证',
            'status' =>'状态',
            'condition' =>'正则表达式',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _filter($map){
        return $map;
    }
    //
    protected function _after_list($list){
        $mArr = model('Module')->where('fid', '<>', 0)->column('title', 'id');
        foreach($list as $key => $row){
            $list[$key]['module_id'] = $mArr[$row['module_id']] ?? '';
            $list[$key]['type'] = $row['type'] == '0' ? '是' : '否';
            $list[$key]['status'] = $row['status'] == '1' ? '可用' : '禁用';
        }
        return $list;
    }
    //
    protected function _before_add(){
        $fArr = model('Module')->where('fid', '=', 0)->field('id,title')->select()->toArray();
        $mArr = [];
        $sonArr = [];
        if($fArr){
            $module = model('Module');
            foreach($fArr as $row){
                $tmp = $module->where('fid', '=', $row['id'])->field('id,title')->select()->toArray();
                $mArr[$row['title']] = $tmp;
                foreach($tmp as $row1){
                    $sonArr[$row1['id']] = $module->where('fid', '=', $row1['id'])->field('title,module')->select()->toArray();
                }
            }
        }
        $this->assign('mArr', $mArr);
        $this->assign('sonArr', $sonArr);
    }
    //
    protected function _before_insert($data){
        return $data;
    }
    //
    protected function _after_add($data, $id){
        $this->log(['pk' => 'id', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _filter_edit($list){
        return $list;
    }
    //
    protected function _before_save($data){
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'id', 'value' => $data['id']], $data);
    }
    //
    protected function _filter_del(){
        return [];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'id', 'value' => $row['id'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}